package dk.hewison.client.panels;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Image;
import dk.hewison.client.panels.ImagePanel;
import dk.hewison.client.Log;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 11:50:20 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 79 $:
 */
public class ImageStack extends DeckPanel {

    private String[] imagesNames;
    private ImagePanel[] nEffectPanels;

    int index = -1;
    private Timer timer;

    public ImageStack(String[] imagesNames) {
        this.imagesNames = imagesNames;


        init();
    }

    private void init() {
        nEffectPanels = new ImagePanel[imagesNames.length];
        for (int i = 0; i < imagesNames.length; i++) {
            Image image = new Image(imagesNames[i]);
            nEffectPanels[i] = new ImagePanel(this, image);
            add(nEffectPanels[i]);
        }

        timer = new Timer() {
            @Override
            public void run() {
                next();
            }
        };
        timer.scheduleRepeating(4000);
        timer.run();
    }

    private void next() {
        if (index == -1) {
            imageIsHidden();
        }
        ImagePanel imagePanel = ((ImagePanel) getWidget(index));
        if (imagePanel == null) {
            imageIsHidden();
        } else {
            imagePanel.hideImage();
        }

    }

    public void imageIsHidden() {
        index++;
        if (index > imagesNames.length - 1) {
            index = 0;
        }
        Log.debug("ImageStack.next " + index);
        showWidget(index);
        ((ImagePanel) getWidget(index)).show();
    }

    public void stop() {
        timer.cancel();
    }
}
